package nl.hanze.monoalphabetic.ui;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import nl.hanze.monoalphabetic.controller.MainController;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

/**
 *
 * @author alex
 */
public class ImportPanel extends JPanel {

    private final JFileChooser jFileChooser;
    private MainController mainController;
    private DefaultCategoryDataset dataset;
    private Font labelFont;

    public ImportPanel(MainController mainController) {
        this.mainController = mainController;
        labelFont = new Font("Courier new", Font.BOLD, 14);

        setLayout(null);
        addButtonPanel();
        addGraphPanel();
        setVisible(true);

        jFileChooser = new JFileChooser();
        FileFilter txtFilter = new FileNameExtensionFilter("Text file", "txt");
        jFileChooser.setFileFilter(txtFilter);

    }

    private void addButtonPanel() {
        JButton openButton = new JButton("Open bestand");
        openButton.setBounds(20, 10, 125, 25);
        add(openButton);

        openButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                openFile();
            }
        });


//        JLabel buttonLabel = new JLabel("tetete");
//        buttonLabel.setBounds(40, 40, 100, 25);
//        buttonPanel.add(buttonLabel);

//        add(buttonPanel, BorderLayout.NORTH);
    }

    private void addGraphPanel() {

        dataset = new DefaultCategoryDataset();

        JFreeChart chart = ChartFactory.createBarChart(
                "Frequentieverdeling", // chart title
                "", // domain axis label
                "Relatief aantal", // range axis label
                dataset, // data
                PlotOrientation.VERTICAL, // orientation
                true, // include legend
                true, // tooltips?
                false // URLs?
                );


        ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setBounds(20, 50, 800, 600);
        add(chartPanel);
    }

    public void addFrequencies() {
        int vDistance = 20;
        //float[] frequencyTable = mainController.getTotalFreqTabel();

        JLabel labFreq = new JLabel("Frequentie");
        labFreq.setBounds(850, 40, 150, 25);
        add(labFreq);

        for (int i = 0; i < 26; i++) {
            JLabel label = new JLabel(((char) ('a' + i)) + " => "
                    + mainController.getReferenceStatistics().getSingleCharacter().get(i).getFormattedFrequency());
            label.setBounds(850, i * vDistance + 60, 150, 25);
            label.setFont(labelFont);
            add(label);
        }
        repaint();
    }

    public void addStatistics() {
        int vDistance = 20;

        JLabel labTwoLC = new JLabel("2LC");
        labTwoLC.setBounds(950, 40, 180, 25);
        add(labTwoLC);

        int i = 0;
        for (String s : mainController.getReferenceStatistics().getTopTwoLetterCombinations()) {
            JLabel label = new JLabel(s);
            label.setBounds(950, i * vDistance + 60, 150, 25);
            label.setFont(labelFont);
            add(label);
            i++;
        }
        
        JLabel labThreeLC = new JLabel("3LC");
        labThreeLC.setBounds(1000, 40, 180, 25);
        add(labThreeLC);

        i = 0;
        for (String s : mainController.getReferenceStatistics().getTopThreeLetterCombinations()) {
            JLabel label = new JLabel(s);
            label.setBounds(1000, i * vDistance + 60, 150, 25);
            label.setFont(labelFont);
            add(label);
            i++;
        }
        
        JLabel labTwoLetter = new JLabel("2LW");
        labTwoLetter.setBounds(1050, 40, 180, 25);
        add(labTwoLetter);

        i = 0;
        for (String s : mainController.getReferenceStatistics().getTopTwoLetterWords()) {
            JLabel label = new JLabel(s);
            label.setBounds(1050, i * vDistance + 60, 150, 25);
            label.setFont(labelFont);
            add(label);
            i++;
        }
        JLabel labThreeLetter = new JLabel("3LW");
        labThreeLetter.setBounds(1100, 40, 180, 25);
        add(labThreeLetter);
        i = 0;
        for (String s : mainController.getReferenceStatistics().getTopThreeLetterWords()) {
            JLabel label = new JLabel(s);
            label.setBounds(1100, i * vDistance + 60, 150, 25);
            label.setFont(labelFont);
            add(label);
            i++;
        }

    }

    public void openFile() {
        int result = jFileChooser.showOpenDialog(null);
        if (result == JFileChooser.APPROVE_OPTION) {
            File file = jFileChooser.getSelectedFile();
            mainController.analyzeFile(file);
            addData();
            addFrequencies();
            addStatistics();
        }
    }

    public void addData() {
        dataset.clear();
        for (int i = 0; i < 26; i++) {
            dataset.addValue(mainController.getReferenceStatistics().getSingleCharacter().get(i).getFrequency(), "referentie", "" + (char) ('a' + i));
        }
    }
}
